4
4
.
.
1
1
.
.
5
5
T
T
e
e
x
x
t
t
F
F
i
i
e
e
l
l
d
d
I
I
n
n
f
f
o
o
[
[
R
R
]
]
TextField allows user to enter text.
In order for us to be able to read entered text we need to provide @State Variable as the second Parameter.
When user enters text this Variable is automatically updated with the entered text.
So by reading this @State Variable we can get the text that was entered by the User.
We can also change this @State Variable and then this value will be shown to the User inside the Text Field.
If @State Variable is not initialized then value of the first Parameter is displayed inside the Text Field grayed out.
Otherwise initial value of the @State Variable is displayed to the User inside the Text Field.
www.iosfonts.com contains list of available custom fonts.
Errors: Text Field not reacting to input
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example @State Variable is not initialized so first Parameter "Enter name..." is initially displayed. We also have:
Text View which reads and displays @State Variable name as you type into TextField
Button which print enter value to console and also sets new value
ContentView.swift
import SwiftUI
struct ContentView : View {
@State var name = "" //If not initialized default (first parameter) value is displayed
var body : some View {
VStack(alignment: .leading) {
TextField("Enter name", text: $name)
Text ("Your username: \(name)")
Button ("Button") {
print(self.name)
self.name = "Thank you"
}
}.padding()
}
}
Initial Screen Entered name Button pressed